//
//  CUtillty.c
//  V-POS
//
//  Created by handpay on 12-11-6.
//  Copyright (c) 2012年 handpay. All rights reserved.
//

#include <stdio.h>
/**
 * HEX解码 将形如0x12 0x2A 0x01 转换为122A01
 */
int hexDecode(const unsigned char *e ,int le, char *str)
{
    char data[] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',};
	
	int i;
	int j = 0;
	unsigned char c;
	
	for(i = 0;i< le; i++){
		c = *e++;
		str[j++] = data[((c>>4)&0x0F)];
		str[j++] = data[c&0x0F];
	}
    str[j] = '\0';
	
	return j;
}

/**
 * HEX编码 将形如122A01 转换为0x12 0x2A 0x01
 */
int hexEncode(const char* data, const int len, char* des)
{
	char* p = des;
	int j = 0;
	unsigned char hi;
	unsigned char low;
	
	for(int i = 0;i < len;){
		hi = data[i++];
		if( hi>='0' && hi<='9'){
			hi -= '0';
		}else if(hi>='A' && hi<='F'){
			hi -= 'A';
			hi += 10;
		}else if(hi>='a' && hi<='f'){
			hi -= 'a';
			hi += 10;
		}else{
			hi = 0;
		}
		low = 0;
		if(i < len){
			low = data[i++];
			if( low>='0' && low<='9'){
				low -= '0';
			}else if(low>='A' && low<='F'){
				low -= 'A';
				low += 10;
			}else if(low>='a' && low<='f'){
				low -= 'a';
				low += 10;
			}
		}
		p[j++] = ((hi&0x0F)<<4) | (low&0x0F);
		
	}
	
	return j;
}